package neichuan.server.message;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import neichuan.message.MessageDecoder;
import neichuan.message.MessageEncoder;
import neichuan.server.Server;

/**
 * 需要与客户端保持持久的连接
 */
public class RegisterServer extends Server {
    private static final int MAX_FRAME_LENGTH = 1024*1024*20;
    private static final int LENGTH_FIELD_LENGTH = 4;
    private static final int LENGTH_FIELD_OFFSET = 0;
    private static final int LENGTH_ADJUSTMENT = 0;
    private static final int INITIAL_BYTES_TO_STRIP = 4;

    public RegisterServer(int port) {
        super(port);
    }

    @Override
    protected void setChildHandler(ServerBootstrap sb) {
        sb.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new MessageEncoder());
                ch.pipeline().addLast(new MessageDecoder(MAX_FRAME_LENGTH,LENGTH_FIELD_OFFSET,LENGTH_FIELD_LENGTH,LENGTH_ADJUSTMENT,INITIAL_BYTES_TO_STRIP));
                ch.pipeline().addLast(new RegisterServerHandler());
            }
        });
    }

}
